import service from '../service/index'
const APP_LOGIN_USER='APP_LOGIN_USER'
const Authorization='Authorization'
//验证用户信息
export function AuthLogin() {
  let loginUserStr = sessionStorage.getItem(APP_LOGIN_USER)
  if(loginUserStr){
    return true
  }
  return false
}
//保存用户信息
export function SavaLoginUserInfo(user){
  sessionStorage.setItem(APP_LOGIN_USER,JSON.stringify(user))
}
//查看用户信息
export function getLogin() {
  let userStr = sessionStorage.getItem(APP_LOGIN_USER)
  if(userStr){
    return JSON.parse(userStr)
  }
  return null
}
//清空用户信息
export function LoginOut(){
  sessionStorage.clear()
}
export function saveLoginToken(token){
    sessionStorage.setItem(Authorization,token)
}
export function getLoginToken(){
    return sessionStorage.getItem(Authorization)
}
//获取当前用户的所有权限
export function getLoginUserAllPer(){
  let userKey = getLoginToken()
  let loginUserPerStr = sessionStorage.getItem('LOGIN_USER_PER')
  if(loginUserPerStr){
    return Promise.resolve(JSON.parse(loginUserPerStr))
  }
  return service.loadUserAllPer({key:userKey}).then((res)=>{
    sessionStorage.setItem('LOGIN_USER_PER',JSON.stringify(res.data))
    return res.data
  })
}